﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GS.Config;
using GS.CMS.Entities;
using System.Data;

public partial class usercontrols_login : System.Web.UI.UserControl
{
    string strSessionUser = Config.SessionUser;
    Users _Users = new Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            //System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            //string sRet = oInfo.Name;
            CheckUserLogin();
        }
    }
    /// <summary>
    /// Check login to enable control or disable
    /// </summary>
    private void CheckUserLogin()
    {
        if (Session[strSessionUser] == null)
        {
            lblUserOrGuest.Text = "<a href=\"login.aspx\">Sign in</a>";
            pnOn.Visible = true;
        }
        else
        {
            DataTable dtUser = (DataTable)Session[strSessionUser];
            //Hiển thị tên người đăng nhập
            lblUserOrGuest.Text = string.Format("<a href=\"my-account.aspx\">{0} {1}</a>", dtUser.Rows[0]["LastName"] + "", dtUser.Rows[0]["FirstName"] + "");
            btnRegister.Text = "Logout";
            pnOn.Visible = false;
        }
    }
    /// <summary>
    /// Check User login by Email and Password
    /// </summary>
    /// <param name="Email"></param>
    /// <param name="Password"></param>
    /// <param name="Checked"></param>
    /// <returns></returns>
    private string Login(string Email, string Password)
    {
        string strResult = "", errorLogin="", errorDenied="", wrongPass="";
        DataTable dtUsers = null;
        try
        {
            Session[strSessionUser] = null;
            _Users.Email = Email;
            _Users.Password = Password;
            dtUsers = _Users.Login();
            //Check Login
            if (dtUsers == null)
            {
                strResult = "failed";
                Response.Redirect("login.aspx", true);
                //errorLogin = GetLocalResourceObject("ErrorLogin").ToString();
            }
            else if (dtUsers.Rows.Count > 0 && dtUsers.Columns.Count == 2)
            {
                strResult = "wrongpass";
                Response.Redirect("login.aspx", true);
                //wrongPass = string.Format(GetLocalResourceObject("WrongPass").ToString(), dtUsers.Rows[0]["LoginTimeExits"]);
            }
            else if (dtUsers.Rows.Count > 0 && dtUsers.Columns.Count > 2)
            {
                Session["UserInfo"] = dtUsers;
                strResult = "success";
                CheckUserLogin();
            }
            else
            {
                strResult = "denied";
                errorDenied = GetLocalResourceObject("errorDenied").ToString();
                Response.Redirect("login.aspx", false);
            }
            return strResult;
        }
        catch (Exception ex)
        {
            TrackingError.AddTrackingError("Login Page - Function: Login", ex.Message);
        }
        return strResult;
    }
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
        Login(txtEmail.Text.Trim(),txtPassword.Text.Trim());
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (btnRegister.Text == "Logout")
        {
            Session.Remove(strSessionUser);
            CheckUserLogin();
        }
        else
            Response.Redirect("register.aspx",false);
    }
}